/**
 * Created by 落叶的思恋 on 2017/6/7.
 */
app.controller("index-content", function ($http, $scope, $location) {
    if (JSON.parse(sessionStorage.getItem("user")) != null) {
        $scope.user = JSON.parse(sessionStorage.getItem("user"));
    } else {
        $scope.user = null;
    }

    $scope.bookList = [];
    $scope.bookScience = [];
    $scope.bookBiology = [];
    $scope.bookbiography = [];
    $scope.bookElectrical = [];
    $http({
        method: 'Get',
        url: app.url + 'Book/list'
    }).then(function (res) {

        $scope.bookList = res.data;
        console.log($scope.bookList);
        if ($scope.bookList.length > 0) {
            for (var i = 0; i < $scope.bookList.length; i++) {
                if ($scope.bookList[i].borrownum == null) {
                    $scope.bookList[i].borrownum = 0;
                }
                $scope.bookList[i].bookimg = "../../resources/img/" + $scope.bookList[i].bookimg;
                $scope.bookList[i].width = ($scope.bookList[i].bookNum - $scope.bookList[i].borrownum) / $scope.bookList[i].bookNum * 100 + '%';
                var n = $scope.bookList[i].categoryId;
                if (n == 1) {
                    $scope.bookScience[$scope.bookScience.length] = $scope.bookList[i];
                }
                if (n == 2) {
                    $scope.bookBiology[$scope.bookBiology.length] = $scope.bookList[i];
                }
                if (n == 4) {
                    $scope.bookElectrical[$scope.bookElectrical.length] = $scope.bookList[i];
                }
                if (n == 5) {
                    $scope.bookbiography[$scope.bookbiography.length] = $scope.bookList[i];
                }
                console.log($scope.bookList[i].width)

            }

        }
    }, function () {

    });

    $scope.updatebook = function (book) {
        console.log(book);
        sessionStorage.setItem("book", JSON.stringify(book));
        $location.path("/bookinfo");
    };
    $scope.deletebook = function (bookid) {
        $http({
            method: 'POST',
            url: app.url + '/Book/delete',
            data: {"bookId": bookid}
        }).then(function (res) {
            if (res.data == 0) {
                $("#modal-191634").click();
                $('.modal-title').html("删除失败");
                $('.modal-body').html("服务器响应超时");
                $location.path("/index-content");
            }

        }, function () {
            $("#modal-191634").click();
            $('.modal-title').html("删除失败");
            $('.modal-body').html("服务器响应超时");
            $location.path("/index-content");
        })
    };
    $scope.searchvar=null;

    $scope.searchClick=function () {
        if($scope.searchvar!=null){
            sessionStorage.setItem("search",$scope.searchvar);
            console.log(sessionStorage.getItem("search"));
            $location.path("/search");
        }else{
            return;
        }
    }
});